class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """
        #叶子节点
        if root is None:
            return root
        
        #找到p或q则返回
        if root == p or root == q:
            return root
        
        left = self.lowestCommonAncestor(root.left, p, q)
        right = self.lowestCommonAncestor(root.right, p, q)

        #p、q在左右子树中
        if left is not None and right is not None:
            return root
        
        #p、q都在左子树
        if left is not None and right is None:
            return left
        
        #p、q都在右子树
        if right is not None and left is None:
            return right
